package com.business_clean.ui.activity.custom;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.callback.OnDialogConfirmListener;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.data.mode.custom.CustomMangerDetailEntity;
import com.business_clean.data.mode.custom.CustomMangerList;
import com.business_clean.databinding.ActivityCustomDetailBinding;
import com.business_clean.ui.activity.project.CompanyProjectDetailActivity;
import com.business_clean.ui.adapter.custom.CustomMangerAllAdapter;
import com.business_clean.viewmodel.request.CustomViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.Nullable;

public class CustomDetailActivity extends BaseActivity<CustomViewModel, ActivityCustomDetailBinding> {

    private CustomMangerAllAdapter mAdapter;

    private CustomMangerList customMangerList;

    private int page = 1;

    private String uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            CustomMangerList customMangerList = (CustomMangerList) getIntent().getExtras().getSerializable("custom");
            if (customMangerList != null) {
                uuid = customMangerList.getUuid();
                mViewModel.requestDetails(customMangerList.getUuid());
                mDatabind.tvCustomDetailName.setText(customMangerList.getCustom_name());
                mDatabind.tvCustomDetailSubName.setText(customMangerList.getCustom_full_name());
                mViewModel.getProjectMangerAllList(page, customMangerList.getUuid());
            }
        }
        mAdapter = new CustomMangerAllAdapter();
        mDatabind.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recyclerview.setAdapter(mAdapter);

        mAdapter.setEmptyView(R.layout.layout_base_empty);
    }

    @Override
    public void initObserver() {
        App.appViewModelInstance.getRefreshCustomManagerList().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                mViewModel.requestDetails(uuid);
            }
        });
    }

    @Override
    public void onBindViewClick() {
        //跳转到项目详情
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                ActivityForwardUtil.startActivity(CompanyProjectDetailActivity.class, bundle);
            }
        });
        //删除
        mDatabind.butCustomDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showGeneralDialog(CustomDetailActivity.this, "删除", "删除后不可恢复，确定删除吗？",
                        "取消", "确定", null, new OnDialogConfirmListener() {
                            @Override
                            public void onConfirm() {

                            }
                        });
            }
        });
        //编辑
        mDatabind.butCustomEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("custom", customMangerList);
                ActivityForwardUtil.startActivity(AddCustomActivity.class, bundle);
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getCooperateList().observe(this, new Observer<CustomMangerDetailEntity>() {
            @Override
            public void onChanged(CustomMangerDetailEntity customMangerEntity) {
                mAdapter.setList(customMangerEntity.getList());
            }
        });
        mViewModel.getDelete().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshCustomManagerList().setValue(true);
                finish();
            }
        });

        mViewModel.getCustomDetail().observe(this, new Observer<CustomMangerList>() {
            @Override
            public void onChanged(CustomMangerList data) {
                customMangerList = data;
                mDatabind.tvCustomDetailName.setText(data.getCustom_name());
                mDatabind.tvCustomDetailSubName.setText(data.getCustom_full_name());
            }
        });
    }
}